public class LSt2_2 {

}

class Solution2 {
    public static void main(String[] args) {
        ListNode list1 = new ListNode();
        ListNode list2 = new ListNode();
        int arr[] = {2, 4, 3};
        int brr[] = {5, 5, 4};
        for (int a : arr) {
            list1.add(list1, a);
        }
        for (int a : brr) {
            list2.add(list2, a);
        }
        list1 = list1.next;
        list2 = list2.next;
        System.out.println(list1);
        System.out.println(list2);
        System.out.println("******");


        ListNode list3 = addTwoNumbers(list1, list2);
        System.out.println(list3);
        //ListNode list4 = new ListNode();
    }

    public static ListNode addTwoNumbers(ListNode list1, ListNode list2) {
        root list3 = new root();
        while (true) {
            if (list1 == null && list2 == null) {
                break;
            }
            int a = list1 == null ? 0 : list1.val;
            int b = list2 == null ? 0 : list2.val;
            int num = a + b;

            if (num > 9) {

            } else {
                list3.add(num);
            }
            System.out.println(list3);

            if (list1.next != null) {
                list1 = list1.next;
            } else {
                list1 = null;
            }
            if (list2.next != null) {
                list2 = list2.next;
            } else {
                list2 = null;
            }
        }

        return list3.hand.next;

    }
}

class root {
    ListNode hand = new ListNode(0);

    public void add(int i) {
        ListNode tmp = hand;
        while (true) {
            if (tmp.next == null) {
                break;
            } else {
                tmp = tmp.next;
            }

        }
        tmp.next = new ListNode(i);
    }

    @Override
    public String toString() {
        return hand.toString();
    }
}